home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / dev / lang / bcpl4amiga.lha / bcpl / interp.asm < prev    next >
Assembly Source File  |  1991-02-01  |  9KB  |  536 lines

  1. double    macro
  2.     dad h
  3.     endm
  4. stvar    macro    var,val
  5.     lxi h,val ! shld var
  6.     endm
  7. memr    macro    index
  8.     lhld index ! double
  9.     xchg ! lhld mem ! dad d
  10.     endm
  11. stih    macro    dest
  12.     mov a,m ! inx h ! mov h,m ! mov l,a ! shld dest
  13.     endm
  14. binop    macro op
  15.     lhld @b ! xchg ! lhld @a ! call op
  16.     endm
  17. comhl    macro
  18.     mov a,l ! cma ! mov l,a ! mov a,h ! cma ! mov h,a
  19.     endm
  20. neghl    macro
  21.     local    neg
  22.     mov a,l ! cma ! inr a ! mov l,a ! mov a,h ! cma
  23.     jnc neg ! inr a
  24. neg:    mov h,a
  25.     endm
  26. logop    macro op
  27.     lhld @b ! xchg ! lhld @a
  28.     mov a,h ! op d ! mov h,a
  29.     mov a,l ! op e ! mov l,a
  30.     endm
  31. ;int interpret()
  32. ;{
  33.     extrn    mem,progvec,gp,pp,cycleco
  34.  
  35. ;    register int    a, b, c, d, w;
  36.     dseg
  37. @a:    dw    0
  38. @b:    dw    0
  39. @c:    dw    0
  40. @d:    dw    0
  41. @w:    dw    0
  42.  
  43.     cseg
  44.     public    interpret
  45. interpret:
  46. ;    c = progvec;
  47.     lhld progvec ! shld @c
  48. ;    cyclecount = 0;
  49.     stvar cycleco,0
  50. ;fetch:
  51. fetch:
  52. ;    cyclecount++;
  53.     lhld cycleco ! inx h ! shld cycleco
  54. ;    w = mem[c];
  55. ;    c++
  56.     lhld @c ! inx h ! shld @c ! dcx h ! double
  57.     xchg ! lhld mem ! dad d ! stih @w
  58. ;
  59. ;    if (w & DBIT)
  60. ; save a copy of w in bc
  61.     mov b,h ! mov c,l ! mov a,h ! ora a ! jp @1
  62. ;    {
  63. ;        d = mem[c];
  64. ;        c++;
  65.     lhld @c ! inx h ! shld @c ! dcx h ! double
  66.     xchg ! lhld mem ! dad d ! stih @d
  67.     jmp @2
  68. ;    }
  69. ;    else
  70. @1:
  71. ;        d = w & ABITS;
  72.     mov l,c ! mvi h,0 ! shld @d
  73. @2:
  74. ;
  75. ;#ifdef    DEBUG
  76. ;    fprintf(stderr, "%04x: %04x %05d\n", c - progvec - 1, w, d);
  77. ;#endif    DEBUG
  78. ;
  79. ; hl contains @d at this point
  80. ; b contains a copy of whi at this point
  81.     mov a,b ! rar
  82. ;    if (w & PBIT)
  83.     jnc @3
  84. ;        d += pp;
  85.     xchg ! lhld pp ! dad d ! shld @d
  86. @3:    rar
  87. ;    if (w & GBIT)
  88.     jnc @4
  89. ;        d += gp;
  90.     xchg ! lhld gp ! dad d ! shld @d
  91. @4:    rar
  92. ;    if (w & IBIT)
  93.     jnc    @5
  94. ;        d = mem[d];
  95.     double
  96.     xchg ! lhld mem ! dad d
  97.     mov b,a ! stih @d
  98.     mov a,b
  99. ;
  100. ;    switch ((w >> FSHIFT) & 07)
  101. @5:    ani 0eh ! jz op@l ! mov e,a ! mvi d,0 ! lxi h,@tab1 ! dad d
  102.     mov a,m ! inx h ! mov h,m ! mov l,a ! pchl
  103. @tab1:    dw    op@l
  104.     dw    op@s
  105.     dw    op@a
  106.     dw    op@j
  107.     dw    op@t
  108.     dw    op@f
  109.     dw    op@k
  110.     dw    op@x
  111. ;    {
  112. ;    case OP_L:
  113. op@l:
  114. ;        b = a;
  115. ;        a = d;
  116. ;        goto fetch;
  117.     lhld @a ! shld @b ! lhld @d ! shld @a ! jmp fetch
  118. ;
  119. ;    case OP_S:
  120. op@s:
  121. ;        mem[d] = a;
  122. ;        goto fetch;
  123.     memr @d
  124.     lda @a ! mov m,a ! lda @a+1 ! inx h ! mov m,a ! jmp fetch
  125. ;
  126. ;    case OP_A:
  127. op@a:
  128. ;        a += d;
  129. ;        goto fetch;
  130.     lhld @d ! xchg ! lhld @a ! dad d ! shld @a ! jmp fetch
  131. ;
  132. ;    case OP_J:
  133. op@j:
  134. ;        c = d;
  135. ;        goto fetch;
  136.     lhld @d ! shld @c ! jmp fetch
  137. ;
  138. ;    case OP_T:
  139. op@t:
  140. ;        if (a)
  141. ;            c = d;
  142. ;        goto fetch;
  143.     lhld @a ! mov a,h ! ora l ! jnz op@j ! jmp fetch
  144. ;
  145. ;    case OP_F:
  146. op@f:
  147. ;        if (!;a)
  148. ;            c = d;
  149. ;        goto fetch;
  150.     lhld @a ! mov a,h ! ora l ! jz op@j ! jmp fetch
  151. ;
  152. ;    case OP_K:
  153. op@k:
  154. ;        d += pp;
  155.     lhld pp ! xchg ! lhld @d ! dad d ! shld @d
  156. ;        mem[d] = pp;
  157.     double
  158.     xchg ! lhld mem ! dad d
  159.     lda pp ! mov m,a ! inx h ! lda pp+1 ! mov m,a ! inx h
  160. ;        mem[d+1] = c;
  161.     lda @c ! mov m,a ! inx h ! lda @c+1 ! mov m,a
  162. ;        pp = d;
  163.     lhld @d ! shld pp
  164. ;        c = a;
  165.     lhld @a ! shld @c
  166. ;        goto fetch;
  167.     jmp fetch
  168. ;
  169. ;    case OP_X:
  170. op@x:
  171. ;        switch (d)
  172.     lhld @d ! double
  173.     lxi d,@tab2 ! dad d
  174.     mov a,m ! inx h ! mov h,m ! mov l,a ! pchl
  175. @tab2:    dw    fetch
  176.     dw    x1
  177.     dw    x2
  178.     dw    x3
  179.     dw    x4
  180.     dw    x5
  181.     dw    x6
  182.     dw    x7
  183.     dw    x8
  184.     dw    x9
  185.     dw    x10
  186.     dw    x11
  187.     dw    x12
  188.     dw    x13
  189.     dw    x14
  190.     dw    x15
  191.     dw    x16
  192.     dw    x17
  193.     dw    x18
  194.     dw    x19
  195.     dw    x20
  196.     dw    x21
  197.     dw    x22
  198.     dw    x23
  199.     dw    x24
  200.     dw    x25
  201.     dw    x26
  202.     dw    x27
  203.     dw    x28
  204.     dw    x29
  205.     dw    x30
  206.     dw    x31
  207.     dw    x32
  208.     dw    x33
  209.     dw    x34
  210.     dw    x35
  211.     dw    x36
  212.     dw    x37
  213. ;        {
  214. ;        case 1:
  215. x1:
  216. ;            a = mem[a];
  217.     memr @a
  218.     stih @a
  219.     jmp fetch
  220. ;            goto fetch;
  221. ;        case 2:
  222. x2:
  223. ;            a = -a;
  224.     lhld @a ! neghl
  225.     shld @a ! jmp fetch
  226. ;            goto fetch;
  227. ;        case 3:
  228. x3:
  229. ;            a = ~a;
  230.     lhld @a ! comhl
  231.     shld @a ! jmp fetch
  232. ;            goto fetch;
  233. ;        case 4:
  234. x4:
  235. ;            c = mem[pp+1];
  236. ;            pp = mem[pp];
  237.     lhld pp ! inx h ! inx h ! double
  238.     xchg ! lhld mem ! dad d
  239.     dcx h ! mov a,m ! sta @c+1 ! dcx h ! mov a,m ! sta @c
  240.     dcx h ! mov a,m ! sta pp+1 ! dcx h ! mov a,m ! sta pp ! jmp fetch
  241. ;            goto fetch;
  242. ;        case 5:
  243. x5:
  244. ;            a *= b;
  245.     extrn c@mult
  246.     binop c@mult
  247.     shld @a ! jmp fetch
  248. ;            goto fetch;
  249. ;        case 6:
  250. x6:
  251. ;            a = b / a;
  252.     extrn c@div
  253.     binop c@div
  254.     shld @a ! jmp fetch
  255. ;            goto fetch;
  256. ;        case 7:
  257. x7:
  258. ;            a = b % a;
  259.     binop c@div
  260.     xchg ! shld @a ! jmp fetch
  261. ;            goto fetch;
  262. ;        case 8:
  263. x8:
  264. ;            a += b;
  265.     lhld @b ! xchg ! lhld @a ! dad d ! shld @a ! jmp fetch
  266. ;            goto fetch;
  267. ;        case 9:
  268. x9:
  269. ;            a = b - a;
  270.     lhld @a ! neghl
  271.     xchg ! lhld @b ! dad d ! shld @a ! jmp fetch
  272. ;            goto fetch;
  273. ;        case 10:
  274. x10:
  275. ;            a = b == a ? ~0 : 0;
  276.     lhld @b ! xchg ! lhld @a
  277.     mov a,l ! cmp e ! jnz false
  278.     mov a,h ! cmp d ! jnz false
  279.     stvar @a,-1
  280.     jmp fetch
  281. false:    stvar @a,0
  282.     jmp fetch
  283. ;            goto fetch;
  284. ;        case 11:
  285. x11:
  286. ;            a = b !;= a ? ~0 : 0;
  287.     lhld @b ! xchg ! lhld @a
  288.     mov a,l ! cmp e ! jnz true
  289.     mov a,h ! cmp d ! jz false
  290. true:    stvar @a,-1
  291.     jmp fetch
  292. ;            goto fetch;
  293. ;        case 12:
  294. x12:
  295. ;            a = b < a ? ~0 : 0;
  296.     extrn c@lt
  297.     binop c@lt
  298.     jz false
  299.     stvar @a,-1
  300.     jmp fetch
  301. ;            goto fetch;
  302. ;        case 13:
  303. x13:
  304. ;            a = b >= a ? ~0 : 0;
  305.     extrn c@ge
  306.     binop c@ge
  307.     jz false
  308.     stvar @a,-1
  309.     jmp fetch
  310. ;            goto fetch;
  311. ;        case 14:
  312. x14:
  313. ;            a = b > a ? ~0 : 0;
  314.     extrn c@gt
  315.     binop c@gt
  316.     jz false
  317.     stvar @a,-1
  318.     jmp fetch
  319. ;            goto fetch;
  320. ;        case 15:
  321. x15:
  322. ;            a = b <= a ? ~0 : 0;
  323.     extrn c@le
  324.     binop c@le
  325.     jz false
  326.     stvar @a,-1
  327.     jmp fetch
  328. ;            goto fetch;
  329. ;        case 16:
  330. x16:
  331. ;            a = b << a;
  332.     extrn c@asl
  333.     binop c@asl
  334.     shld @a ! jmp fetch
  335. ;            goto fetch;
  336. ;        case 17:
  337. x17:
  338. ;            a = b >> a;
  339.     extrn c@asr
  340.     binop c@asr
  341.     shld @a ! jmp fetch
  342. ;            goto fetch;
  343. ;        case 18:
  344. x18:
  345. ;            a &= b;
  346.     logop ana
  347.     shld @a ! jmp fetch
  348. ;            goto fetch;
  349. ;        case 19:
  350. x19:
  351. ;            a |= b;
  352.     logop ora
  353.     shld @a ! jmp fetch
  354. ;            goto fetch;
  355. ;        case 20:
  356. x20:
  357. ;            a ^= b;
  358.     logop xra
  359.     shld @a ! jmp fetch
  360. ;            goto fetch;
  361. ;        case 21:
  362. x21:
  363. ;            a ^= ~b;
  364.     lhld @b ! comhl
  365.     xchg ! lhld @a
  366.     mov a,h ! xra d ! mov h,a
  367.     mov a,l ! xra e ! mov l,a
  368.     shld @a ! jmp fetch
  369. ;            goto fetch;
  370. ;
  371. ;        case 22:
  372. x22:
  373. ;            return (0);         /* finish */
  374.     lxi h,0 ! ret
  375. ;
  376. ;        case 23:
  377. x23:
  378. ;            b = mem[c++];
  379. ;            d = mem[c++];    /* switchon */
  380.     lhld @c ! double
  381.     xchg ! lhld mem ! dad d
  382.     mov a,m ! sta @b ! inx h ! mov a,m ! sta @b+1 ! inx h
  383.     mov a,m ! sta @d ! inx h ! mov a,m ! sta @d+1 ! inx h
  384. while:    xchg ! lhld @b ! mov a,l ! ora h ! jz endwhile
  385. ;            while (b !;= 0)
  386. ;            {
  387. ;                b--;
  388.     dcx h ! shld @b ! xchg
  389. ;                if (a == mem[c++])
  390.     lda @a ! cmp m ! inx h ! jnz skip3
  391.     lda @a+1 ! cmp m ! inx h ! jnz skip2
  392. ;                {
  393. ;                    c = mem[c];
  394. ;                    goto fetch;
  395. ;                }
  396.     stih @c
  397.     jmp fetch
  398. skip2:    inx h ! inx h ! jmp while
  399. skip3:    inx h ! inx h ! inx h ! jmp while
  400. ;                c++;
  401. ;            }
  402. endwhile:
  403. ;            c = d;
  404.     lhld @d ! shld @c ! jmp fetch
  405. ;            goto fetch;
  406. ;
  407. ;/*
  408. ;// cases 24 upwards are only called from the following
  409. ;// hand written intcode library - iclib:
  410. ;
  411. ;//    11 lip2 x24 x4 g11l11 /selectinput
  412. ;//    12 lip2 x25 x4 g12l12 /selectoutput
  413. ;//    13 x26 x4      g13l13 /rdch
  414. ;//    14 lip2 x27 x4 g14l14 /wrch
  415. ;//    42 lip2 x28 x4 g42l42 /findinput
  416. ;//    41 lip2 x29 x4 g41l41 /findoutput
  417. ;//    30 lip2 x30 x4 g30l30 /stop
  418. ;//    31 x31 x4 g31l31 /level
  419. ;//    32 lip3 lip2 x32 g32l32 /longjump
  420. ;//    46 x33 x4 g46l46 /endread
  421. ;//    47 x34 x4 g47l47 /endwrite
  422. ;//    40 lip3 lip2 x35 g40l40 /aptovec
  423. ;//    85 lip3 lip2 x36 x4 g85l85 / getbyte
  424. ;//    86 lip3 lip2 x37 x4 g86l86 / putbyte
  425. ;//    z
  426. ;*/
  427. ;
  428. ;        case 24:
  429. x24:
  430. ;            slctinput(a);
  431.     extrn slctinp
  432.     lhld @a ! push h ! call slctinp ! pop b ! jmp fetch
  433. ;            goto fetch;
  434. ;        case 25:
  435. x25:
  436. ;            slctoutput(a);
  437.     extrn slctout
  438.     lhld @a ! push h ! call slctout ! pop b ! jmp fetch
  439. ;            goto fetch;
  440. ;        case 26:
  441. x26:
  442. ;            a = rdch();
  443.     extrn rdch
  444.     call rdch ! shld @a ! jmp fetch
  445. ;            goto fetch;
  446. ;        case 27:
  447. x27:
  448. ;            wrch(a);
  449.     extrn wrch
  450.     lhld @a ! push h ! call wrch ! pop b ! jmp fetch
  451. ;            goto fetch;
  452. ;        case 28:
  453. x28:
  454. ;            a = findinput(a);
  455.     extrn findinp
  456.     lhld @a ! push h ! call findinp ! pop b ! shld @a ! jmp fetch
  457. ;            goto fetch;
  458. ;        case 29:
  459. x29:
  460. ;            a = findoutput(a);
  461.     extrn findout
  462.     lhld @a ! push h ! call findout ! pop b ! shld @a ! jmp fetch
  463. ;            goto fetch;
  464. ;        case 30:
  465. x30:
  466. ;            return (a);        /* stop(a) */
  467.     lhld @a ! ret
  468. ;        case 31:
  469. x31:
  470. ;            a = mem[pp];
  471.     memr pp
  472.     stih @a
  473.     jmp fetch
  474. ;            goto fetch;        /* used in level() */
  475. ;        case 32:
  476. x32:
  477. ;            pp = a;
  478. ;            c = b;            /* used in longjump(p,l) */
  479.     lhld @a ! shld pp ! lhld @b ! shld @c ! jmp fetch
  480. ;            goto fetch;
  481. ;        case 33:
  482. x33:
  483. ;            endread();
  484.     extrn endread
  485.     call endread ! jmp fetch
  486. ;            goto fetch;
  487. ;        case 34:
  488. x34:
  489. ;            endwrite();
  490.     extrn endwrit
  491.     call endwrit ! jmp fetch
  492. ;            goto fetch;
  493. ;        case 35:
  494. x35:
  495. ;            d = pp+b+1;        /* used in aptovec(f, n) */
  496.     lhld pp ! xchg ! lhld @b ! dad d ! inx h ! shld @d
  497. ;            mem[d] = mem[pp];
  498.     double
  499.     xchg ! lhld mem ! dad d ! push h
  500.     lhld pp ! double
  501.     xchg ! lhld mem ! dad d
  502.     mov c,l ! mov b,h ! pop h
  503.     ldax b ! inx b ! mov m,a ! inx h
  504.     ldax b ! inx b ! mov m,a ! inx h
  505. ;            mem[d+1] = mem[pp+1];
  506.     ldax b ! inx b ! mov m,a ! inx h
  507.     ldax b ! mov m,a ! inx h
  508. ;            mem[d+2] = pp;
  509.     lda pp ! mov m,a ! inx h ! lda pp+1 ! mov m,a ! inx h
  510. ;            mem[d+3] = b;
  511.     lda @b ! mov m,a ! inx h ! lda @b+1 ! mov m,a
  512. ;            pp = d;
  513.     lhld @d ! shld pp
  514. ;            c = a;
  515.     lhld @a ! shld @c ! jmp fetch
  516. ;            goto fetch;
  517. ;        case 36:
  518. x36:
  519. ;            a = icgetbyte(a, b);    /* getbyte(s, i) */
  520.     extrn icgetby
  521.     lhld @a ! push h ! lhld @b ! push h ! call icgetby ! pop b ! pop b
  522.     shld @a ! jmp fetch
  523. ;            goto fetch;
  524. ;        case 37:
  525. x37:
  526. ;            icputbyte(a, b, mem[pp+4]);    /* putbyte(s, i, ch) */
  527.     extrn icputby
  528.     lhld @a ! push h ! lhld @b ! push h ! lhld pp ! lxi d,4
  529.     dad d ! double
  530.     xchg ! lhld mem ! dad d ! mov c,m ! inx h ! mov b,m ! push b
  531.     call icputby ! pop b ! pop b ! pop b ! jmp fetch
  532. ;            goto fetch;
  533. ;        }
  534. ;    }
  535. ;}
  536.